package main

import (
	"context"
	"errors"
	"fmt"
	clientv3 "go.etcd.io/etcd/client/v3"
	"google.golang.org/grpc"
	"time"
)

var CLI *clientv3.Client

// https://pkg.go.dev/go.etcd.io/etcd/client/v3
func init() {
	{
		// expect dial time-out on ipv4 blackhole
		_, err := clientv3.New(clientv3.Config{
			Endpoints:   []string{"http://254.0.0.1:12345"},
			DialTimeout: 2 * time.Second,
		})

		// etcd clientv3 >= v3.2.10, grpc/grpc-go >= v1.7.3
		if errors.Is(err, context.DeadlineExceeded) {
			// handle errors
		}

		// etcd clientv3 <= v3.2.9, grpc/grpc-go <= v1.2.1
		if errors.Is(err, grpc.ErrClientConnTimeout) {
			// handle errors
		}
	}
	cli, err := clientv3.New(clientv3.Config{
		//Endpoints:   []string{"localhost:2379", "localhost:22379", "localhost:32379"},
		Endpoints:   []string{"localhost:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		fmt.Println(err)
	}
	CLI = cli
	//defer cli.Close()
}
